﻿/*
Copyright (c) 2010 The Developer Journey Company

Permission is hereby granted, free of charge, to any person obtaining a copy 
of this software and associated documentation files (the "Software"), to deal 
in the Software without restriction, including without limitation the rights 
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
copies of the Software, and to permit persons to whom the Software is 
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all 
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 
THE SOFTWARE. 
*/

using System;
using System.ComponentModel;
using System.Collections.ObjectModel;
using CommunityMegaphone.Services;
using System.Xml.Linq;

namespace CommunityMegaphone.ViewModels
{
    public class SettingsViewModel : NotifyPropertyChangedBase
    {
        public ObservableCollection<EventTypeViewModel> EventTypes { get; private set; }
        public ObservableCollection<StateViewModel> States { get; private set; }

        public SettingsViewModel()
        {
            EventTypes = new ObservableCollection<EventTypeViewModel>();
            States = new ObservableCollection<StateViewModel>();
            LoadReferenceData();
        }

        public void LoadReferenceData()
        {
            try
            {
                var client = new EventManagerSoapClient("EventManagerSoap");
                client.GetStatesCompleted += client_GetStatesCompleted;
                client.GetStatesAsync();
                client.GetEventTypesCompleted += OnGetEventTypesCompleted;
                client.GetEventTypesAsync();
            }
            catch { }
        }

        void client_GetStatesCompleted(object sender, GetStatesCompletedEventArgs e)
        {
            if (e.Error != null || e.Cancelled || e.Result == null || e.Result.Length == 0)
                return;
            var newStates = new ObservableCollection<StateViewModel>();
            newStates.Add(
                new StateViewModel()
                {
                    Code = "All",
                    Name = "All States"
                });
            foreach (StateItem item in e.Result)
            {
                newStates.Add(
                    new StateViewModel()
                    {
                        Code = item.Code,
                        Name = item.Name
                    });
            }
            States = newStates;
            NotifyPropertyChanged("States");
        }

        void OnGetEventTypesCompleted(object sender, GetEventTypesCompletedEventArgs e)
        {
            if (e.Error != null || e.Cancelled || e.Result == null || e.Result.Length == 0)
                return;
            var newEventTypes = new ObservableCollection<EventTypeViewModel>();
            newEventTypes.Add(
                new EventTypeViewModel()
                { 
                    ID = "All", 
                    InPerson = true, 
                    Name = "All Event Types" 
                });
            foreach (EventTypeItem item in e.Result)
            {
                newEventTypes.Add(
                    new EventTypeViewModel()
                    {
                        ID = item.ID, 
                        InPerson = item.InPerson, 
                        Name = item.Name 
                    });
            }
            EventTypes = newEventTypes;
            NotifyPropertyChanged("EventTypes");
        }
    }
}
